﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;

namespace JQMdotNet
{
    public static partial class JQM
    {
        #region Begin Page Renderers

        public static IHtmlString RenderBeginJQMPage()
        {
            return RenderBeginJQMPage(null, null, new Dictionary<string,object>());
        }

        public static IHtmlString RenderBeginJQMPage(object htmlAttributes)
        {
            return RenderBeginJQMPage(null, null, new RouteValueDictionary(HelperUtilities.Fix_Attributes(htmlAttributes)));
        }

        public static IHtmlString RenderBeginJQMPage(string id, object htmlAttributes)
        {
            return RenderBeginJQMPage(id, null, new RouteValueDictionary(HelperUtilities.Fix_Attributes(htmlAttributes)));
        }

        public static IHtmlString RenderBeginJQMPage(string id, string title, object htmlAttributes)
        {
            return RenderBeginJQMPage(id, title, new RouteValueDictionary(HelperUtilities.Fix_Attributes(htmlAttributes)));
        }

        public static IHtmlString RenderBeginJQMPage(IDictionary<string,object> htmlAttributes)
        {
            return RenderBeginJQMPage(null, null, htmlAttributes);
        }

        public static IHtmlString RenderBeginJQMPage(string id, IDictionary<string,object> htmlAttributes)
        {
            return RenderBeginJQMPage(id, null, htmlAttributes);
        }

        public static IHtmlString RenderBeginJQMPage(string id, string title, IDictionary<string,object> htmlAttributes)
        {
            JQMUtilities.AddRole(htmlAttributes, "page");
            if (!String.IsNullOrEmpty(id))
                JQMUtilities.ReplaceAttibute(htmlAttributes, "id", id);

            if (!String.IsNullOrEmpty(title))
                JQMUtilities.ReplaceAttibute(htmlAttributes, "data-title", title);

            return UniversalTagHelper.RenderBeginTag("div", htmlAttributes);
        }

        #endregion

        #region Universal tag helpers

        public static UniversalTag BeginJQMPage(this HtmlHelper helper)
        {
            return helper.BeginJQMPage(null, null, new Dictionary<string,object>());
        }

        public static UniversalTag BeginJQMPage(this HtmlHelper helper, object htmlAttributes)
        {
            return helper.BeginJQMPage(null, null, new RouteValueDictionary(HelperUtilities.Fix_Attributes(htmlAttributes)));
        }

        public static UniversalTag RenderBeginJQMPage(this HtmlHelper helper, string id, object htmlAttributes)
        {
            return helper.BeginJQMPage(id, null, new RouteValueDictionary(HelperUtilities.Fix_Attributes(htmlAttributes)));
        }

        public static UniversalTag RenderBeginJQMPage(this HtmlHelper helper, string id, string title, object htmlAttributes)
        {
            return helper.BeginJQMPage(id, title, new RouteValueDictionary(HelperUtilities.Fix_Attributes(htmlAttributes)));
        }

        public static UniversalTag RenderBeginJQMPage(this HtmlHelper helper, IDictionary<string,object> htmlAttributes)
        {
            return helper.BeginJQMPage(null, null, htmlAttributes);
        }

        public static UniversalTag BeginJQMPage(this HtmlHelper helper, string id, IDictionary<string,object> htmlAttributes)
        {
            return helper.BeginJQMPage(id, null, htmlAttributes);
        }

        public static UniversalTag BeginJQMPage(this HtmlHelper helper, string id, string title, object htmlAttributes)
        {
            helper.ViewContext.Writer.Write(RenderBeginJQMPage(id, title, htmlAttributes));
            return new UniversalTag(helper, "div");
        }

        public static UniversalTag BeginJQMPage(this HtmlHelper helper, string id, string title, IDictionary<string,object> htmlAttributes)
        {
            helper.ViewContext.Writer.Write(RenderBeginJQMPage(id, title, htmlAttributes));
            return new UniversalTag(helper, "div");
        }

        public static void EndJQMPage(this HtmlHelper helper)
        {
            helper.EndTag("div");
        }
        #endregion
    }

}